package com.situ.feemanagement.controller;

import com.situ.feemanagement.pojo.Auth;
import com.situ.feemanagement.pojo.Department;
import com.situ.feemanagement.pojo.User;
import com.situ.feemanagement.pojo.query.UserQuery;
import com.situ.feemanagement.pojo.vo.UserVO;
import com.situ.feemanagement.service.IAuthService;
import com.situ.feemanagement.service.IDepartmentService;
import com.situ.feemanagement.service.IUserService;
import com.situ.feemanagement.util.CookieUtils;
import com.situ.feemanagement.util.PageResult;
import com.situ.feemanagement.util.Result;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IAuthService authService;

    @RequestMapping("/login")
    public Result login(String name, String password, String autoLogin, String code, HttpServletResponse response, HttpSession session) {
        String codeInSession = (String) session.getAttribute("codeInSession");
        if(!codeInSession.equalsIgnoreCase(code)) {
            return Result.error("验证码错误");
        }

        User user = userService.login(name, password);
        if(user == null) {
            return Result.error("用户名或密码错误");
        }
        if(user.getStatus() == 0) {
            return Result.error("该用户已禁用");
        }

        if(!ObjectUtils.isEmpty(autoLogin)) {
            Cookie cookie = new Cookie("autoLogin",user.getName() + "&" + user.getPassword());
            cookie.setMaxAge(60*60*24*7);
            cookie.setPath("/");
            response.addCookie(cookie);
        }

        session.setAttribute("user", user);
        List<Auth> authList = authService.selectByRole(user.getRole());
        session.setAttribute("authList", authList);
        return Result.ok("登录成功");
    }

    @RequestMapping("/logout")
    public Result logout(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        session.removeAttribute("user");
        session.removeAttribute("auths");
        Cookie cookie = CookieUtils.getCookie(request,"autoLogin");
        if (cookie != null) {
            cookie.setMaxAge(0);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return Result.ok("退出成功");
    }

    @RequestMapping("/selectByPage")
    public PageResult<UserVO> selectByPage(UserQuery userQuery) {
        return userService.selectByPage(userQuery);
    }

    @RequestMapping("/deleteById")
    public Result deleteById(String id) {
        userService.deleteById(Integer.parseInt(id));
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    public Result deleteAll(String[] ids) {
        userService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/add")
    public Result add(User user) {
        userService.add(user);
        return Result.ok("添加成功");
    }

    @RequestMapping("/selectMap")
    public Result selectMap(String id) {
        User user = userService.selectById(Integer.parseInt(id));
        Map<String, Object> map = new HashMap<>();
        List<Department> list = departmentService.selectAll();
        map.put("user", user);
        map.put("department",list);
        return Result.ok(map);
    }

    @RequestMapping("/update")
    public Result update(User user) {
        userService.update(user);
        return Result.ok("修改成功");
    }

    @RequestMapping("/updateStatus")
    public Result updateStatus(String id, String status) {
        userService.updateStatus(Integer.parseInt(id), Integer.parseInt(status));
        return Result.ok("更新成功");
    }

    @RequestMapping("/selectUser")
    public List<User> selectUser() {
        return userService.selectUser();
    }
}
